package com.example.shkjdemo.adapter

import android.app.Activity
import android.content.ActivityNotFoundException
import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.example.shkjdemo.KeyCodeActivity
import com.example.shkjdemo.R
import com.example.shkjdemo.RippleActivity
import com.example.shkjdemo.audio_record.AudioRecordActivity
import com.example.shkjdemo.bean.MainMenuBean
import com.example.shkjdemo.bluetooth.MyBluetoothActivity
import com.example.shkjdemo.camera1.Camera1Activity
import com.example.shkjdemo.camera2.Camera2Activity
import com.example.shkjdemo.cameraX.CameraXActivity
import com.example.shkjdemo.lottie.LottieActivity
import com.example.shkjdemo.picker.PickerActivity
import com.example.shkjdemo.test_recyclerview.HorizontalRecyclerViewActivity
import com.orhanobut.logger.Logger
import com.yelin.taketitle.DrawRectActivity
import com.yelin.taketitle.TakePhotoActivity
import kotlin.system.exitProcess

class MainAdapter : RecyclerView.Adapter<MainAdapter.ViewHolder> {
    private val TAG = "MainAdapter"
    private lateinit var mList: MutableList<MainMenuBean>
    lateinit var mActivity: Activity

    constructor(activity: Activity, mList: MutableList<MainMenuBean>) : super() {
        this.mList = mList
        this.mActivity = activity
    }

    companion object{

    }
    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val tv_item = itemView.findViewById<TextView>(R.id.tv_item)
    }


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MainAdapter.ViewHolder {
        val view =
            LayoutInflater.from(parent.context).inflate(R.layout.item_function_list, parent, false)
        val holder = ViewHolder(view)
        holder.tv_item.setOnClickListener {
            Logger.v("onCreateViewHolder:${holder.position}")
            when (holder.position + 1) {
                1 -> {
//                    Camera1
                    mActivity.startActivity(Intent(mActivity, Camera1Activity::class.java))
                }
                2 -> {
//                      Camera2
                    mActivity.startActivity(Intent(mActivity, Camera2Activity::class.java))
                }
                3 -> {
//                      CameraX
                    Intent(mActivity, CameraXActivity::class.java).apply {
                        mActivity.startActivity(this)
                    }
                }
                4 -> {
//                      录音demo
                    Intent(mActivity, AudioRecordActivity::class.java).apply {
                        mActivity.startActivity(this)
                    }
                }
                5 -> {
                    Intent(mActivity, KeyCodeActivity::class.java).apply {
                        mActivity.startActivity(this)
                    }
                }

                6 -> {
//                    横向list
                    Intent(mActivity, HorizontalRecyclerViewActivity::class.java).apply {
                        mActivity.startActivity(this)
                    }
                }
                7 -> {
//                    二级滚动
                    Intent(mActivity, PickerActivity::class.java).apply {
                        mActivity.startActivity(this)
                    }

                }
                8 -> {
//                    json 动画
                    Intent(mActivity, LottieActivity::class.java).apply {
                        mActivity.startActivity(this)
                    }
                }
                9 -> {
//                    蓝牙
                    Intent(mActivity, MyBluetoothActivity::class.java).apply {
                        mActivity.startActivity(this)
                    }

                }
                10 -> {
                    try {
                        Intent("com.dosmono.intent.photo.translate").apply {
                            putExtra("TRANSLATE", "test001")
                            mActivity.startActivity(this)
                        }

                    }catch (e: ActivityNotFoundException){
                        Logger.e("$TAG,跨进程activity异常")
                    }

                }

                11 -> {
                    Intent(mActivity, TakePhotoActivity::class.java).apply {
                        mActivity.startActivity(this)
                    }
                }
                itemCount ->{
                 exitProcess(0)
                }
                12 -> {
                    Intent(mActivity, DrawRectActivity::class.java).apply {
                        mActivity.startActivity(this)
                    }
                }
                13 -> {
                    Intent(mActivity, RippleActivity::class.java).apply {
                        mActivity.startActivity(this)
                    }
                }
                itemCount ->{
                 exitProcess(0)
                }
            }

        }
        return holder
    }

    override fun getItemCount(): Int {
//        Logger.v("getItemCount:${mList.size}")
        return mList.size
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.tv_item.text = mList[position].item
    }

    override fun getItemViewType(position: Int): Int {
        return position
    }
}